package com.twu.biblioteca;

import java.util.HashMap;
import java.util.Set;

/**
 * Created by isabellali on 4/06/2014.
 */
public class Biblioteca {

    private HashMap<String,Book> bookLibrary;
    private HashMap<String, Movie> movieLibrary;
    private HashMap<String, User> userLibrary;
    private User currentUser;


    Biblioteca(){
        bookLibrary = new HashMap<String, Book>();
        Book HeadFirstJava = new Book("Head First Java", "Kathy Sierra & Bert Bates", 2003);
        Book TestDrivenDev = new Book("Test-Driven Development", "Kent Beck", 2003);
        Book AgileDev = new Book("Don't Make Me Think, Revisited", "Steve Krug", 2014);

        bookLibrary.put(HeadFirstJava.getBookName(),HeadFirstJava);
        bookLibrary.put(TestDrivenDev.getBookName(),TestDrivenDev);
        bookLibrary.put(AgileDev.getBookName(),AgileDev);

        movieLibrary = new HashMap<String, Movie>();
        Movie Barefoot = new Movie("Barefoot", 2014, "Andrew Fleming","unrated");
        Movie Robocop = new Movie("Robocop", 2014, "Jose Padilha", "5");
        movieLibrary.put(Barefoot.getName(), Barefoot);
        movieLibrary.put(Robocop.getName(), Robocop);

        userLibrary = new HashMap<String, User>();
        User user = new User("Isabella","11qazwsx","Issy Li","dnflyangle@gmail.com","0452177317");
        userLibrary.put("Isabella", user);
        currentUser = null;
    }

    public String welcomeMessage(){
        return "Welcome to Biblioteca Library!";
    }

    public Set<String> showBookNameList(){
        return bookLibrary.keySet();
    }

    public HashMap<String,Book> showAllBooksDetails(){
        return bookLibrary;
    }
    public HashMap<String,Movie> showAllMoiveDetails(){ return movieLibrary; }

    public String showInvalidMessage(){
        return "Select a valid option!";
    }

    public boolean validCommand(String command){
        if (command.equals("BL") || command.equals("R") || command.equals("C") ||
                command.equals("S") || command.equals("CM") || command.equals("L"))
            return true;
        else
            return false;
    }

    public String checkOutMovie(Movie movie){
        if(movieLibrary.containsKey(movie.getName())) {
            Movie temp = movieLibrary.get(movie.getName());
            if(!temp.isInLibrary())
                return  "That movie is not available.";
            else{
                temp.setInLibrary(false);
                movieLibrary.put(movie.getName(),temp);
                return "Thank you! Enjoy the movie.";
            }
        }
        return "That movie is not available.";
    }
    public String checkOutBook(Book book){
        if(bookLibrary.containsKey(book.getBookName())) {
            Book temp = bookLibrary.get(book.getBookName());
            if(!temp.isInLibrary())
                return  "That book is not available.";
            else{
                temp.setInLibrary(false);
                bookLibrary.put(book.getBookName(),temp);
                return "Thank you! Enjoy the book.";
            }
        }
        return "That book is not available.";
    }

    public String returnBook(Book book){
        if(bookLibrary.containsKey(book.getBookName())){
            Book temp = bookLibrary.get(book.getBookName());
            if(temp.isInLibrary())
                return "That is not a valid book to return.";
            else{
                temp.setInLibrary(true);
                bookLibrary.put(temp.getBookName(),temp);
                return "Thank you for returning the book.";
            }
        }
        return "That is not a valid book to return.";
    }

    public String returnMovie(Movie movie){
        if(movieLibrary.containsKey(movie.getName())){
            Movie temp = movieLibrary.get(movie.getName());
            if(temp.isInLibrary())
                return "That is not a valid Movie to return.";
            else{
                temp.setInLibrary(true);
                movieLibrary.put(temp.getName(),temp);
                return "Thank you for returning the Movie.";
            }
        }
        return "That is not a valid Movie to return.";
    }

    public boolean userLogin(String username, String password){
        if(userLibrary.containsKey(username)){
            User temp= userLibrary.get(username);
            if (temp.getPassword().equals(password)){
                currentUser = temp;
                return true;
            }
            return false;
        }
        return false;
    }

    public User getCurrentUser() {
        return currentUser;
    }

}
